package com.legolas.config;

import com.legolas.config.Interceptor.CROSSInterceptor;
import com.legolas.config.Interceptor.DemoInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/**
 * Created by Administrator on 2016/5/12 0012.
 */

/**
 * 继承WebMvcConfigurerAdapter,常用的Mvc相关内容都在MyMvcConfig里编写
 */

@Configuration
@EnableWebMvc
@ComponentScan("com.legolas.config")
public class MyMvcConfig extends WebMvcConfigurerAdapter {
    //EnableWebMvc会开启一些默认配置，如一些ViewResolver或者MessageConverter
    //运行代码时页面会自动编译到/WEB-INF/classes/views下，所以这样设置前缀
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("WEB-INF/classes/views/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setViewClass(JstlView.class);
        return viewResolver;
    }


    @Autowired
    DemoInterceptor demoInterceptor;
    @Autowired
    CROSSInterceptor crossInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(crossInterceptor).addPathPatterns("/origin_allow");
        //添加路径拦截失败？
        /*registry.addInterceptor(demoInterceptor);*/
    }

    //如果路径中带.通过重写configurePathMatch方法，不忽略.后面的参数
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);
    }

    //重写addResourceHandlers方法实现程序静态文件的直接访问。
    //addResourceLocations是指文件的放置目录，addResourceHandler是指文件对外暴露的访问路径
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/");
    }

    //为页面添加控制转向，访问路径
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("/index");
        registry.addViewController("/toUpload").setViewName("/upload");
    }



}
